"""
7毫秒解决，自己写的
"""
# class Solution:
#     def digin(self,s):
#         for i in s:
#             if i in '0123456789':
#                 return True #s中有数字
#     def myAtoi(self, s: str) -> int:
#         lis =[]
#         flag =1 #决定返回的数字是否是正数
#         i=0
#         for i in range(len(s)):
#             if s[i].isdigit():
#                lis.append(s[i])
#             elif s[i].isalpha():
#                 break
#             elif s[i] == '+' :
#                 # if "+" in s[:i] or "-" in s[:i] :
#                 if lis != [] or ("." in s[:i] or "-" in s[:i] or "+" in s[:i] or self.digin(s[:i]) ) :
#                     break
#             elif s[i] == "-":
#                 if "-" in s[:i] or "+" in s[:i]:
#                         break
#                 elif not lis == []:
#                     break
#                 elif "-" in s and "+" in s and s.index("-")-s.index("+") in [i,-1]: #+-或-+情况
#                     break
#                 else:
#                     flag = -1
#             elif s[i] == '.':
#                 break
#             elif s[i] == " "and ("." in s[:i] or "-" in s[:i] or "+" in s[:i] or self.digin(s[:i]) ):
#                 break
#
#         if lis ==[]:
#             return 0
#         num = 0
#         for i in lis:
#             num = num * 10 + int(i)
#         num = num*flag
#         if num > 2**31-1:
#             return 2**31-1
#         if num < -2**31:
#             return -2**31
#         return num
#
# # print(Solution().myAtoi(" ++1"))

"""
0毫秒解决，来自力扣
"""
class Solution:
    def myAtoi(self, s: str) -> int:
        s = s.lstrip()
        if not s:
            return 0
        sign = 1
        if s[0] == '-':
            sign = -1
            s = s[1:]
        elif s[0] == '+':
            s = s[1:]

        num_str = ''
        for char in s:
            if char.isdigit():
                num_str += char
            else:
                break

        if not num_str:
            return 0
        num_int = int(num_str) * sign

        if num_int > 2 ** 31 - 1:
            return 2 ** 31 - 1
        elif num_int < -2 ** 31:
            return -2 ** 31
        else:
            return num_int

print(Solution().myAtoi(' +-1 2  +'))


